package com.yorick.authority.controller;

import com.yorick.authority.enums.Account;
import com.yorick.authority.modal.request.account.*;
import com.yorick.authority.modal.response.account.AccountEditResponse;
import com.yorick.authority.modal.response.BaseListResponse;
import com.yorick.authority.service.impl.AccountServiceImpl;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 账户管理
 * @author: Yorick
 * @create: 2022-05-15 17:30
 **/
@Slf4j
@Controller
@RequestMapping("account")
public class AccountController {
    @Resource
    private AccountServiceImpl accountsService;

    //新增用户
    @RequestMapping(value = "addAccount", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> addAccount(@Valid @RequestBody AccountAddRequest requestBody) {
        return accountsService.addAccount(requestBody);
    }

    //用户列表
    @RequestMapping(value = "accountList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> accountList(@Valid @RequestBody AccountListRequest requestBody,
                                           HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        requestBody.setSuperRole(Boolean.parseBoolean(session.getAttribute(Account.superRole.name()).toString()));
        BaseListResponse result = accountsService.accountList(requestBody);
        return ResultFactory.success(result).display();
    }

    //用户详情
    @RequestMapping(value = "accountDetail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> accountDetail(@Valid @RequestBody AccountDetailRequest requestBody) {
        AccountEditResponse result = accountsService.accountDetail(requestBody);
        return ResultFactory.success(result).display();
    }

    //编辑用户
    @RequestMapping(value = "accountEdit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> accountEdit(@Valid @RequestBody AccountEditRequest requestBody) {
            return accountsService.editAccount(requestBody);
    }

    //修改密码(需要旧密码)
    @RequestMapping(value = "changePassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> changePassword(@Valid @RequestBody AccountChangePasswordRequest requestBody,
                                              HttpServletRequest request) {
            HttpSession session = request.getSession();
            requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
            requestBody.setAccount(session.getAttribute(Account.account.name()).toString());
            return accountsService.editPassword(requestBody);
    }


    //禁用启用账户
    @RequestMapping(value = "accountStatus", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> accountStatus(@Valid @RequestBody AccountChangeStatusRequest requestBody) {
        return accountsService.editAccountStatus(requestBody);

    }
}
